package com.gouwudai.gobbs.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.extremecomponents.table.limit.Limit;

import com.gouwudai.gobbs.common.Constants;
import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IClassUserDAO;
import com.gouwudai.gobbs.pojo.TbbsClassUser;
import com.gouwudai.gobbs.service.IClassUserService;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.util.ExtremeTableUtil;

/**
 * 
 *  <b>Title:</b> 版面成员表(认证用户\版主)
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-6-20
 * @version 1.0
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class ClassUserServiceImpl implements IClassUserService
{
    IClassUserDAO classUserDAO;
    
    /**
     * 保存版面成员信息.
     * 
     * @param tbbsClassUser 
     * 
     */
    public void save(TbbsClassUser tbbsClassUser) throws BusiException
    {
        if(new Integer(2).equals(tbbsClassUser.getUserType()))  //成员类型为版主
        {
            boolean isClassManager = classUserDAO.isClassManager(tbbsClassUser.getClassId(),tbbsClassUser.getUserId());
            if(isClassManager)
                throw new BusiException("该用户已经是该版的版主！");
        }
        classUserDAO.save(tbbsClassUser);
    }
    
    /**
     * 批量保存版面成员
     * @param tbbsClassUsers
     */
    public void saveBatch(List tbbsClassUsers)
    {           
        for(int i=0;i<tbbsClassUsers.size();i++)
        {
            TbbsClassUser tbbsClassUser = (TbbsClassUser)tbbsClassUsers.get(i);
            classUserDAO.save(tbbsClassUser);       
        }
        
    } 
    
    /**
     * 删除版面成员信息
     * @param cuId
     */
    public void delete(Integer cuId)
    {
        classUserDAO.delete(cuId);
    }
    
    /**
     * 批量删除版面成员信息
     * @param cuIds
     */
    public void deleteBatch(String cuIds)
    {
        classUserDAO.deleteBatch(cuIds);
    }
    
    /**
     * 查询分类/版面版主
     * @param classId
     * @return List
     */
    public List listClassManager(Integer classId)
    {
        return classUserDAO.listClassManager(classId);
    }
    
    /**
     * 查询分类/版面认证用户
     * @param classId
     * @return List
     */
    public List listClassAuthUser(Integer classId)
    {
        return classUserDAO.listClassAuthUser(classId);
    }  
    
    /**
     * 查询指定用户是否被指定版面认证过.
     * 
     * @param classId 
     * @param userId 
     * 
     * @return List
     */
    public boolean hasClassAuth(Integer classId,Integer userId)
    {
        return classUserDAO.hasClassAuth(classId, userId);
    }
    
    /**
     * 当前用户是否是分类/版面管理员
     * @param classId
     * @param userId
     * @return boolean
     */
    public boolean isClassManager(Integer classId,Integer userId)
    {
        return classUserDAO.isClassManager(classId, userId);
    }
    
    /**
     * 分页查询
     * @param request
     */
    public void listClassUserByPage(javax.servlet.http.HttpServletRequest request)
    {
        Map<String,Object> conds = new HashMap<String,Object>();
        conds.put("loginId", Utility.trimNull(request.getParameter("loginId")));
        conds.put("classId", Utility.parseInt(request.getParameter("classId")));  
        conds.put("userType", Utility.parseInt(request.getParameter("userType")));
        
        Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
        IPageList page = classUserDAO.listClassUserByPage(new PageParam(conds,limit.getPage(), limit.getCurrentRowsDisplayed(),ExtremeTableUtil.getSort(limit)));
        request.setAttribute("resultList",page.getRsList());
        request.setAttribute("totalRows",page.getTotalSize());       
    }
    
    /**
     * 查询所有没有设置过版面用户的有效用户.
     * 
     * @param userType 版面用户类型: 1-认证用户,2-版主
     * @param classId 版面ID
     * 
     * @return the list
     */
    public List listAllUserClassNotHave(Integer classId,Integer userType)
    {
        return classUserDAO.listAllUserClassNotHave(classId, userType);
    }
    
    

    public void setClassUserDAO(IClassUserDAO classUserDAO)
    {
        this.classUserDAO = classUserDAO;
    }
    
}
